
import { parseTime } from '@/utils'
export default {
  data() {
    return {
      caledarMinDate: '',
      caledarMaxDate: '',
      caledar: {
        show: false,
        title: '',
        field: '', // 当前字段
        defaultData: []
      },
      datetimePicker: {
        show: false,
        title: '',
        field: '',
        keyName: '',
        keyValue: ''
      }
    }
  },
  created() {
    const date = new Date()
    const mDate = date.setFullYear(date.getFullYear() - 6)
    this.caledarMinDate = new Date(mDate)
    this.caledarMaxDate = new Date()
  },
  methods: {
    // 打开日历选择器
    openCaledar(f) {
      let defVal = []
      if (f.value1 && f.value2) {
        defVal = [new Date(f.value1), new Date(f.value2)]
      }
      if (defVal.length === 0) {
        const d = new Date()
        defVal = [d, d]
      }
      this.caledar = {
        show: true,
        title: f.title,
        field: f.flag,
        defaultData: defVal
      }
    },
    // 确认日历选择
    onConfirmCaledar(date) {
      const [start, end] = date
      const value1 = parseTime(start, '{y}-{m}-{d}')
      const value2 = parseTime(end, '{y}-{m}-{d}')
      const field = this.caledar.field
      const index = this.filterData.findIndex(v => v.flag === field)
      this.$set(this.filterData[index], 'value1', value1)
      this.$set(this.filterData[index], 'value2', value2)

      // this.selectedClick(this.filterData[index], this.filtersView)
      this.$nextTick(() => {
        this.caledar = {
          show: false,
          field: '',
          defaultData: []
        }
      })
    },
    // 更新值
    changeFieldValues(f, keyName) {
      const index = this.filterData.findIndex(v => v.flag === f.flag)
      this.$set(this.filterData[index], keyName, f[keyName])
    }
  }
}
